<?php
//正则工具类开发

class regexTool{
	private $validate = array(
		'require' => '/.+/',
		'email' => '/^\w+(\.\w+)*@\w+(\.\w+)+$/',
		'url' => '/^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$/',
		'currency' => '/^\d+(\.\d+)?$/',
		'number' => '/^\d+$/',
		'zip' => '/^\d{6}$/',
		'integer' => '/^[-\+]?\d+$/',
		'double' => '/^[-\+]?\d+(\.\d+)?$/',
		'english' => '/^[A-za-z]+$/',
		'qq' => '/^\d{5,11}$/',
		'mobile' => '/^1(3|4|5|7|8)\d{9}$/'
	);
	private $returnMatchResult = false;//定义类的最后返回结果是匹配到的数据或者验证结果的真伪
	private $fixMode = null;//定义修正模式
	private $matches = array();//存放匹配到的结果
	private $isMatch = false;//是否匹配成功


	//构造函数
	public function _construct($returnMatchResult = false,$fixMode = null){
		$this->returnMatchResult = $returnMatchResult;
		$this->fixMode = $fixMode;
	}


	//核心匹配方法
	private function regex($pattern,$subject){
		if(array_key_exists(strtolower($pattern), $this->validate)){
			$pattern = $this->validate[$pattern].$this->fixMode;
		}
		$this->returnMatchResult ?
			preg_match_all($pattern, $subject,$this->$matches):
			$this->isMatch = preg_match($pattern, $subject)===1;
		return $this->getRegexResult();
	}
	private function getRegexResult(){
		if($this->returnMatchResult){
			return $this->matches;
		}else{
			return $this->isMatch;
		}
	}

	public function toggleReturnType($bool = null){
		if(empty($bool)){
			$this->returnMatchResult = !$this->returnMatchResult;
		}else{
			$this->returnMatchResult = is_boll($bool)?$bool : (bool)$bool;
		}
	}

	//设置修正模式
	public function setFixMode($fixMode){
		$this->fixMode = $fixMode;
	}




	//数据验证的方法

	public function noEmpty($str){
		return $this->regex('require',$str);
	}

	public function isEmail($email){
		return $this->regex('email',$email);
	}

	

	public function isMobile($mobile){
		return $this->regex('email',$mobile);
	}

	//用户自定义正则表达式
	public function check($pattren,$subject){
		return $this->regex($pattren,$subject);
	}

}




?>